package id.artefact.hidayatullah;

import id.artefact.hidayatullah.help.BitmapDecoder;
import id.artefact.hidayatullah.help.EntryAdapter;
import id.artefact.hidayatullah.help.EntryItem;
import id.artefact.hidayatullah.help.Item;
import id.artefact.hidayatullah.help.LazyAdapterBehindMenu;
import id.artefact.hidayatullah.help.SectionItem;

import java.io.File;
import java.util.ArrayList;

import org.xmlrpc.android.FileHelper;

import com.actionbarsherlock.app.SherlockListFragment;

import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class BehindFragment extends SherlockListFragment {
	public final static String KEY_TITLE = "title";
	public final static String KEY_THUMB_URL = "thumb_url";

	ArrayList<Item> items = new ArrayList<Item>();

	LazyAdapterBehindMenu adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list_behind, null);

	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		FrameLayout bm = (FrameLayout) getActivity().findViewById(
				R.id.menu_frame);
		Resources res = this.getResources();
		int id = R.drawable.back_behind;
		Bitmap b = BitmapFactory.decodeResource(res, id, null);
		Drawable dr = new BitmapDrawable(res, b);
		bm.setBackgroundDrawable(dr);
		items.add(new EntryItem(R.drawable.search, "Search"));
		items.add(new EntryItem(R.drawable.terkini, "Terkini"));
		items.add(new EntryItem(R.drawable.popular, "Spesial"));
		items.add(new SectionItem("Category"));
		items.add(new EntryItem(R.drawable.category, "Berita"));
		items.add(new EntryItem(R.drawable.category, "Kajian"));
		items.add(new EntryItem(R.drawable.category, "Artikel"));
		items.add(new EntryItem(R.drawable.category, "Kolom"));
		items.add(new EntryItem(R.drawable.category, "Iptekes"));
		items.add(new EntryItem(R.drawable.category, "Konsultasi"));
		items.add(new EntryItem(R.drawable.category, "Redaksi"));
		items.add(new EntryItem(R.drawable.about, "About"));
		items.add(new EntryItem(R.drawable.web, "Mobile Site"));
		items.add(new EntryItem(R.drawable.email, "Feedback"));
		EntryAdapter adapter = new EntryAdapter(getListView().getContext(),
				items);
		getListView().setAdapter(adapter);
	}

	@Override
	public void onListItemClick(ListView lv, View v, int position, long id) {
		Fragment newContent = null;
		switch (position) {
		case 0:
			searchPopUp();
			break;
		case 1:
			// Intent i = new Intent(getActivity(), MainActivity.class);
			// startActivity(i);
			newContent = new AboveFragment("terkini");
			break;
		case 2:
			 newContent = new AboveCategory("51", "Spesial");
			break;
		case 4:
			 newContent = new AboveCategory("8", "Berita");
			break;
		case 5:
			 newContent = new AboveCategory("26", "Kajian");
			break;
		case 6:
			 newContent = new AboveCategory("7", "Artikel");
			break;
		case 7:
			 newContent = new AboveCategory("30", "Kolom");
			break;
		case 8:
			 newContent = new AboveCategory("23", "Iptekes");
			break;
		case 9:
			 newContent = new AboveCategory("31", "Konsultasi");
			break;
		case 10:
			 newContent = new AboveCategory("46", "Redaksi");
			break;
		case 11:
			Intent j = new Intent(getActivity(), AboutActivity.class);
			startActivity(j);
			break;
		case 12:
			GoTo("Web");
			break;
		case 13:
			GoTo("Email");
			break;
		}
		if (newContent != null)
			switchFragment(newContent);
	}

	// the meat of switching the above fragment
	private void switchFragment(Fragment fragment) {
		if (getActivity() == null)
			return;

		if (getActivity() instanceof MainActivity) {
			MainActivity fca = (MainActivity) getActivity();
			fca.switchContent(fragment);
		} else {
			MainActivity fca = (MainActivity) getActivity();
			fca.switchContent(fragment);
		}
	}

	private void GoTo(String Var) {
		if (Var.equals("Web")) {
			Intent l = new Intent(Intent.ACTION_VIEW,
					Uri.parse("http://www2.hidayatullah.com"));
			startActivity(l);
		} else if (Var.equals("Email")) {
			Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
			emailIntent.setType("plain/text");
			String aEmailList[] = { "redaksi-online@hidayatullah.com" };
			emailIntent
					.putExtra(android.content.Intent.EXTRA_EMAIL, aEmailList);
			emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
					"Feedback");
			emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
					"Tuliskan Saran untuk Redaksi Kami");
			startActivityForResult(emailIntent, 0);
		}
	}

	public void searchPopUp() {

		LayoutInflater inflater = (LayoutInflater) getLayoutInflater(getArguments());
		final View element = inflater
				.inflate(R.layout.form_search, null, false);
		final EditText txtSearch = (EditText) element.findViewById(R.id.search);
		final View header = inflater.inflate(R.layout.dialog_customtitle, null);
		TextView tx = (TextView) header.findViewById(R.id.text_title);
		tx.setText("Search");
		new AlertDialog.Builder(getActivity()).setView(element)
				.setCustomTitle(header)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
					@TargetApi(11)
					public void onClick(DialogInterface dialog, int id) {
						if (txtSearch.getText().toString() != null) {

							Log.d("texxxxxxxxxxxxxxxt...", txtSearch.getText()
									.toString());
							Fragment newContent = new AboveSearch(
									"Hasil Pencarian", txtSearch.getText()
											.toString());
							if (newContent != null)
								switchFragment(newContent);
						}
						dialog.cancel();
					}

				}).show();
	}

}
